<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商家名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商家名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家ID" prop="name">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入商家ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户ID" prop="name">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="类型" prop="name">
        <el-select v-model="queryParams.source" placeholder="请选择类型">
          <el-option label="不限制" :value="null"></el-option>
          <el-option label="线上商家" :value="1"></el-option>
          <el-option label="线下商家" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="name">
        <el-select v-model="queryParams.toExamine" placeholder="请选择类型">
          <el-option label="待审核" :value="0"></el-option>
          <el-option label="审核通过" :value="1"></el-option>
          <el-option label="审核不通过" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="display: block">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ds:store:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ds:store:edit']"
          >审核</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ds:product:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="storeList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家ID" align="center" prop="id" />
      <el-table-column label="商家名" align="center" prop="name" />
      <el-table-column label="用户ID" align="center" prop="uid" />
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.source === 1 ? "线上" : "线下" }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
      <el-table-column label="商家图片" align="center" prop="picture">
        <template slot-scope="scope">
          <img
            style="width: 80px; height: 80px; border: none"
            :src="scope.row.picture"
          />
        </template>
      </el-table-column>
      <el-table-column label="法人代表" align="center" prop="legalPerson" />
      <el-table-column
        label="地址"
        align="center"
        prop="address"
        min-width="150"
      />
      <el-table-column
        label="添加时间"
        align="center"
        prop="createTime"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上架/下架"
        align="center"
        class-name="small-padding fixed-width"
        width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            inactive-color="#13ce66"
            active-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="statusChecked($event, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="审核状态" align="center" prop="toExamine">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.toExamine == 0" type="warning">未审核</el-tag>
          <el-tag v-if="scope.row.toExamine == 1" type="success"
            >审核通过</el-tag
          >
          <el-tag v-if="scope.row.toExamine == 2" type="danger"
            >拒绝通过</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
        min-width="280"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ds:store:edit']"
            >查看详情</el-button
          >

          <el-button
            type="primary"
            @click="handleEdit(scope.row)"
            v-hasPermi="['ds:store:edit']"
          >
            商家编辑
          </el-button>

          <el-button
            type="primary"
            @click="handleDefinite(scope.row)"
            v-hasPermi="['ds:store:edit']"
          >
            商家明细
          </el-button>
        </template>
      </el-table-column>

      <!-- 质押商铺 -->
      <el-table-column
        label="质押"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="openPledge(scope.row)"
            :type="scope.row.pledge <= 1000 ? 'primary' : 'info'"
            v-hasPermi="['ds:store:edit']"
          >
            {{ scope.row.pledge <= 1000 ? "质押商铺" : "已冻结" }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="编辑商家信息"
      :visible.sync="editShow"
      width="800px"
      append-to-bod
      :close-on-click-modal="false"
      @close="editDialogClosed"
    >
      <el-form ref="editForm" :model="editForm" label-width="80px">
        <el-form-item label="商家名称">
          <el-input
            v-model="editForm.name"
            placeholder="请输入商家名"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <ImageUpload v-model="editForm.picture"></ImageUpload>
        </el-form-item>
        <el-form-item label="地址">
          <el-input
            v-model="editForm.address"
            placeholder="请输入商家地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input
            v-model="editForm.telephone"
            placeholder="请输入联系方式"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类Id">
          <el-select v-model="editForm.classification" placeholder="请选择分类">
            <el-option label="美食餐饮" :value="290"></el-option>
            <el-option label="美容养生" :value="291"></el-option>
            <el-option label="装修建材" :value="292"></el-option>
            <el-option label="日用家居" :value="293"></el-option>
            <el-option label="休闲娱乐" :value="294"></el-option>
            <el-option label="酒店民宿" :value="295"></el-option>
            <el-option label="超市商场" :value="296"></el-option>
            <el-option label="生活服务" :value="297"></el-option>
            <el-option label="运动健身" :value="298"></el-option>
            <el-option label="汽车美容" :value="299"></el-option>
          </el-select>
          <!-- <el-input
            v-model="editForm.classification"
            placeholder="请输入商家分类Id"
            type="number"
          ></el-input> -->
        </el-form-item>
        <el-form-item label="线上/线下" prop="name">
          <el-select v-model="editForm.source" placeholder="请选择类型">
            <el-option label="线上" :value="1"></el-option>
            <el-option label="线下" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editShow = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加或修改商家信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-bod
      :close-on-click-modal="false"
      @close="dialogClosed"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="商家名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商家名" />
        </el-form-item>
        <el-form-item label="商家详情">
          <editor v-model="form.details" :min-height="192" />
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input v-model="form.uid" placeholder="用户ID" />
        </el-form-item>
        <el-form-item label="分类ID">
          <el-select v-model="form.classification" placeholder="请选择分类">
            <el-option label="美食餐饮" :value="290"></el-option>
            <el-option label="美容养生" :value="291"></el-option>
            <el-option label="装修建材" :value="292"></el-option>
            <el-option label="日用家居" :value="293"></el-option>
            <el-option label="休闲娱乐" :value="294"></el-option>
            <el-option label="酒店民宿" :value="295"></el-option>
            <el-option label="超市商场" :value="296"></el-option>
            <el-option label="生活服务" :value="297"></el-option>
            <el-option label="运动健身" :value="298"></el-option>
            <el-option label="汽车美容" :value="299"></el-option>
          </el-select>
          <!-- <el-input v-model="form.classification" placeholder="商家分类Id" /> -->
        </el-form-item>
        <el-form-item label="线上/线下" prop="name">
          <el-select v-model="form.source" placeholder="请选择类型">
            <el-option label="线上" :value="1"></el-option>
            <el-option label="线下" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="法人代表">
          <el-input v-model="form.legalPerson" placeholder="" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.telephone" placeholder="" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" placeholder="" />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="身份证正面">
              <imageUpload v-model="form.frontIdCard" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="身份证背面">
              <imageUpload v-model="form.reverseIdCard" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="营业执照">
              <imageUpload v-model="form.businessLicense" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="20">
            <el-form-item label="备注">
              <editor v-model="form.remarks" :min-height="130" />
            </el-form-item>
          </el-col>
          <el-col :span="4" style="color: red">
            (开发者寄语)
            审核人员请注意，如您拒绝通过，请再原备注另起一行，确保输入原因，再点击拒绝按钮
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitAddStore">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="提现列表" border :visible.sync="stIdShow" width="1400">
      <div class="dialog-export">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport(stIdList[0])"
          >导出</el-button
        >

        <DatePicker v-model="queryTime" :timevalue1="queryTime"></DatePicker>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="dialogQuery(stIdList[0])"
          style="margin-left: 30px"
          >搜索</el-button
        >
        <!-- <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport(stIdList[0])"
          >搜索</el-button
        > -->
      </div>

      <!--       :before-close="handleWithdrawalClose" -->
      <el-table :data="stIdList" v-loading="stIdLoading">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="id" label="ID" width="90"></el-table-column>
        <el-table-column
          prop="stid"
          label="商家ID"
          width="90"
        ></el-table-column>
        <el-table-column prop="type" label="类型" width="90"></el-table-column>
        <el-table-column
          prop="inOut"
          label="支出/收入"
          width="90"
        ></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="amount" label="金额"></el-table-column>
        <el-table-column prop="balance" label="用户余额"></el-table-column>
        <el-table-column
          prop="detailed"
          label="详情"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="买入时间"
          width="150"
        ></el-table-column>
        <!-- <el-table-column
          prop="updateBy"
          label="银行卡号"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="createBy"
          label="真实姓名"
          width="150"
        ></el-table-column> -->
        <!-- <el-table-column
          prop="searchValue"
          label="银行名称"
          width="150"
        ></el-table-column> -->
        <el-table-column
          prop="remark"
          label="备注"
          width="150"
        ></el-table-column>
        <!-- <el-table-column prop="createTime" label="时间"></el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="id" label="Id"></el-table-column>
        <el-table-column prop="inOut" label="类型"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="amount" label="金额"></el-table-column>
        <el-table-column prop="balance" label="余额"></el-table-column>
        <el-table-column prop="createTime" label="提交时间"></el-table-column>
        <el-table-column prop="name" label="区域"></el-table-column>
        <el-table-column prop="detailed" label="详细信息"></el-table-column> -->
        <!-- <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column> -->

        <!-- <el-table-column
          prop="status"
          width="220"
          label="操作"
          v-if="detailedFlag"
        >
          <template slot-scope="scope">
            <el-button type="primary" @click="pass(scope.row)">通过</el-button>
            <el-button type="danger" @click="refuse(scope.row)">拒绝</el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <el-pagination
        background
        layout="prev, pager, next"
        :total="stIdTotal"
        :page-size="10"
        :current-page="stIdPageNum"
        @current-change="stIdPageChange"
      >
      </el-pagination>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStore,
  getStore,
  delStore,
  addStore,
  updateStore,
  exportStore,
  pledgeStore,
  storeEdit,
  storeJoin,
  stIdList,
  updateStatueStore,
  storeExport,
} from "@/api/ds/store";
import DatePicker from "@/components/DatePicker";

import ImageUpload from "@/components/ImageUpload";
import ImagesUpload from "@/components/ImagesUpload";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
export default {
  name: "Product",
  components: {
    ImageUpload,
    ImagesUpload,
    Editor,
    DatePicker,
  },
  data() {
    var validateID = (rule, value, callback) => {
      if (value) {
        if (!/^([1-9][0-9]*){1,3}$/.test(value)) {
          callback(new Error("请输入正确的值"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入值"));
      }
    };
    var validatePrice = (rule, value, callback) => {
      if (value) {
        if (!/^([1-9][0-9]*)+(.[0-9]{1,2})?$/.test(value)) {
          callback(new Error("请输入正确的价格（最多两位小数）"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入价格"));
      }
    };
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/uploadImage", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },

      filesList: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家信息表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      editShow: false,
      //查询时间
      queryTime: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        toExamine: 1,
      },
      // 表单参数
      form: {},

      editForm: {},

      // 明细列表
      stIdList: [],
      stIdShow: false,
      stIdTotal: 0,
      stIdPageNum: 1,
      queryObj: null,
      stIdLoading: true,

      // 表单校验
      rules: {
        image: [{ required: true, message: "商家图不能为空", trigger: "blur" }],
        sliderImage: [
          { required: true, message: "轮播图不能为空", trigger: "blur" },
        ],
        name: [{ required: true, message: "商家名不能为空", trigger: "blur" }],
        cateId: [{ required: true, validator: validateID, trigger: "blur" }],
        price: [{ required: true, message: "进价不能为空", trigger: "blur" }],
        vipPrice: [
          { required: true, message: "售价不能为空", trigger: "blur" },
        ],
        sales: [{ required: true, message: "销量不能为空", trigger: "blur" }],
        stock: [{ required: true, message: "库存不能为空", trigger: "blur" }],
        quota: [{ required: true, message: "贡献值不能为空", trigger: "blur" }],
        giftAmount: [
          { required: true, message: "赠送倍数不能为空", trigger: "blur" },
        ],
        limitNum: [
          { required: true, message: "限购数量不能为空", trigger: "blur" },
        ],
      },
      isMixPay: false, // 是否混合支付
      toExamineOptions: [
        // 分类下拉框
        {
          value: "0",
          label: "未审核",
        },
        {
          value: "1",
          label: "审核通过",
        },
        {
          value: "2",
          label: "拒绝通过",
        },
      ],
    };
  },
  created() {
    this.getList();
  },

  methods: {
    async submitAddStore() {
      console.log(this.form);
      let res = await storeJoin(this.form);
      if (res.code === 200) {
        this.open = false;
        this.$notify({
          type: "success",
          message: res.msg,
          title: "成功",
        });
        this.getList();
      }
    },
    /** 查询商家信息列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then((response) => {
        this.storeList = response.rows;
        console.log(this.storeList);
        this.total = response.total;
        this.loading = false;
      });
    },
    dialogClosed() {
      this.open = false;
      this.$refs.upload.clearFiles(); // 取消按钮也要清一遍轮播图
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    dialogQuery(row) {
      console.log(row);
      this.stIdPageNum = 1;
      this.stIdLoading = true;
      this.stIdShow = true;
      this.queryObj = row;
      console.log(row);
      let query = {
        stid: row.stid,
        pageNum: this.stIdPageNum,
        pageSize: 10,
      };
      query = this.addDateRange(query, this.queryTime, "");
      this.detailedFlag = true;

      stIdList(query).then((res) => {
        this.stIdLoading = false;
        this.stIdList = res.rows;
        this.stIdTotal = res.total;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    // 操作上下架switch按钮
    statusChecked(e, row) {
      row.status = e;
      let query = {
        id: row.id,
        status: row.status,
      };
      updateStatueStore(query).then((response) => {
        console.log(response);
        this.msgSuccess("修改成功");
        this.getList();
      });
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isMixPay = false;
      this.open = true;
      this.title = "添加商家信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStore(id).then((response) => {
        console.log(
          "%c [ response ]-473",
          "font-size:13px; background:#333333; color:#f3f3f3;",
          response
        );
        this.form = response.data;
        this.open = true;
        this.title = "审核商家";
      });
    },

    /**
     * 修改商家细腻些
     */
    handleEdit(row) {
      // 处理编辑商家
      this.editShow = true;
      this.editForm = row;
    },

    // 明细
    async handleDefinite(row) {
      this.stIdLoading = true;
      this.stIdShow = true;
      this.queryObj = row;
      console.log(row);
      let query = {
        stid: row.id,
        pageNum: this.stIdPageNum,
        pageSize: 10,
      };
      this.detailedFlag = true;

      let res = await stIdList(query);
      this.stIdLoading = false;
      this.stIdList = res.rows;
      this.stIdTotal = res.total;
    },
    stIdPageChange(val) {
      console.log(val);
      this.stIdPageNum = val;

      this.handleDefinite(this.queryObj);
    },

    editDialogClosed() {
      this.editShow = false;
      this.$refs.upload.clearFiles(); // 取消按钮也要清一遍轮播图
      this.reset();
    },

    async submitEdit() {
      let res = await storeEdit(this.editForm);
      if (res.code === 200) {
        console.log(res);
        this.$notify({
          title: "成功",
          message: res.msg,
          type: "success",
        });

        this.editForm = {};
        this.editShow = false;

        this.getList();
      }
    },

    /** 导出按钮操作 */
    handleExport(row) {
      console.log(row);
      if (!row) {
        this.$message.error("没有可以导出的数据");
        return false;
      }
      let queryParams = {
        stid: row.stid,
      };
      queryParams = this.addDateRange(queryParams, this.queryTime, "");
      console.log(queryParams);
      this.$confirm("是否确认导出所有订单数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return storeExport(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    /** 当关闭混合支付按钮时，gtfy支付比例归0 */
    payTypeChanged() {
      if (!this.isMixPay) {
        this.form.passProportion = 0;
        this.form.cashRatio = 100;
      }
    },
    // 质押加个
    openPledge(row) {
      if (row.pledge <= 1000) {
        // console.log(row);
        this.$prompt("请输入质押金额", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPattern:
            /^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*)|0)$/,
          inputErrorMessage: "请输入正确金额",
        })
          .then(({ value }) => {
            pledgeStore({ id: row.id, price: value }).then((res) => {
              console.log(res);
              if (res === 200) {
                this.pledge = false;
                this.$message({
                  type: "success",
                  message: "您的质押金额是: " + value,
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消质押",
            });
          });
      } else {
        this.$message({
          type: "info",
          message: "已冻结",
        });
      }
    },
  },
};
</script>
<style>
.actualPay {
  width: 33%;
  text-align: center;
  margin-left: 6%;
  display: inline-block;
  color: white;
  background: #1890ff;
  font-weight: 600;
  border-radius: 4px;
}
.dialog-export {
  display: flex;
  /* justify-content: space-around; */
  align-items: center;
}
.dialog-export button {
  margin-right: 30px;
}
</style>
